package world.criver.mm_vworld_server.construct;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import world.criver.mm_vworld_server.data.daSave;
import world.criver.mm_vworld_server.data.daServer;
import world.criver.mm_vworld_server.function.funClient;
import world.criver.mm_vworld_server.function.funSave;
import world.criver.mm_vworld_server.function.funServer;
import world.criver.mm_vworld_server.function.funUDP;
import world.criver.mm_vworld_server.modular.moClient;

import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import static world.criver.mm_vworld_server.data.daServer.clients;

@Component
public class consTimer {
    @Autowired
    private funUDP funUDP;
    @Autowired
    private funSave funSave;
    @Autowired
    private funClient funClient;
    @Autowired
    private funServer funServer;

    private int interval=0;
    private List<Integer> month_big=List.of(1,3,5,7,8,10,12);
    private Random random=new Random();
    private int randomNum=0;

    /**
     * 开服后广播消息
     */
    @PostConstruct
    public void startBroadcast(){
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                if(daServer.server==null || daServer.server.isClosed()){
                    funUDP.close();
                    return;
                }
                funUDP.broadcast();
            }
        },0,1_000);
    }

    /**
     * 向客户端发送心跳
     */
    @PostConstruct
    public void sendClientHeart(){
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                long t=System.currentTimeMillis();
                for(String id: clients.keySet()){
                    if(funClient.send(id,"h#"+t)) {
                        clients.get(id).setHeart(clients.get(id).getHeart() + 1);
                        if (clients.get(id).getHeart() > 2) funServer.stopClient(id);
                    }else funServer.stopClient(id);
                }
            }
        },0,10_000);
    }
    /**
     * 计算时间
     */
    @PostConstruct
    public void autoCountDateTime(){
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                if(daServer.server==null || daServer.server.isClosed())return;
                //分钟增加
                daSave.dateTime.setMinute(daSave.dateTime.getMinute()+15);
                //小时增加
                if(daSave.dateTime.getMinute()>60){
                    daSave.dateTime.setHour(daSave.dateTime.getHour()+1);
                    daSave.dateTime.setMinute(0);
                    countWeather();
                }
                //日期增加
                if(daSave.dateTime.getHour()>=24){
                    daSave.dateTime.setDate(daSave.dateTime.getDate()+1);
                    daSave.dateTime.setHour(0);
                    saveOfUnit("day");
                }
                //月份增加
                if(daSave.dateTime.getMonth()!=2){
                    if(daSave.dateTime.getDate()>(month_big.contains(daSave.dateTime.getMonth())?31:30)){
                        daSave.dateTime.setMonth(daSave.dateTime.getMonth()+1);
                        daSave.dateTime.setDate(1);
                        saveOfUnit("month");
                        countSeason();
                    }
                }else{
                    if((daSave.dateTime.getYear()%100==0 && daSave.dateTime.getYear()%400==0)||(daSave.dateTime.getYear()%100!=0&&daSave.dateTime.getYear()%4==0)){
                        if(daSave.dateTime.getDate()>29){
                            daSave.dateTime.setMonth(daSave.dateTime.getMonth()+1);
                            daSave.dateTime.setDate(1);
                            saveOfUnit("month");
                            countSeason();
                        }
                    }else{
                        if(daSave.dateTime.getDate()>28){
                            daSave.dateTime.setMonth(daSave.dateTime.getMonth()+1);
                            daSave.dateTime.setDate(1);
                            saveOfUnit("month");
                            countSeason();
                        }
                    }
                }
                //年份增加
                if(daSave.dateTime.getMonth()>12){
                    daSave.dateTime.setMonth(1);
                    daSave.dateTime.setYear(daSave.dateTime.getYear()+1);
                    saveOfUnit("year");
                }
                Thread.startVirtualThread(new TimerTask() {
                    @Override
                    public void run() {
                        for(String id: clients.keySet())
                            funClient.send(id,"5#"+daSave.dateTime.getYear()+"&"+daSave.dateTime.getMonth()+"&"+daSave.dateTime.getDate()+"&"+daSave.dateTime.getHour()+"&"+daSave.dateTime.getMinute()
                                    +"&"+daSave.dateTime.getSeason()+"&"+daSave.dateTime.getWeather());
                    }
                });
            }
        },0,15_000);
    }
    private void saveOfUnit(String unit){
        if(daSave.properties.isAutoSaveOpen()&&daSave.properties.getAutoSaveUnit().equals(unit)){
            interval++;
            if(daSave.properties.getAutoSaveInterval()>=interval){
                interval=0;
                Thread.startVirtualThread(()->{funSave.save();});
            }
        }
    }
    private void countSeason(){
        if(daSave.dateTime.getMonth()>=3&&daSave.dateTime.getMonth()<=5)daSave.dateTime.setSeason(0);
        else if(daSave.dateTime.getMonth()>=6&&daSave.dateTime.getMonth()<=8)daSave.dateTime.setSeason(1);
        else if(daSave.dateTime.getMonth()>=9&&daSave.dateTime.getMonth()<=11)daSave.dateTime.setSeason(2);
        else daSave.dateTime.setSeason(3);
    }

    /**
     * 计算天气
     */
    private void countWeather(){
        daSave.dateTime.setWeatherTimeOfDuration(daSave.dateTime.getWeatherTimeOfDuration()-1);
        if(daSave.dateTime.getWeatherTimeOfDuration()>0)return;
        randomNum=random.nextInt(100);
        switch (daSave.dateTime.getSeason()){
            case 0->{//春天
                //多雨月份
                if(daSave.dateTime.getMonth()==5){
                    if(randomNum>60)daSave.dateTime.setWeather(1);
                    else if(randomNum>50)daSave.dateTime.setWeather(3);
                    else daSave.dateTime.setWeather(0);
                    daSave.dateTime.setWeatherTimeOfDuration(random.nextInt(24)+8);
                }else{
                    if(randomNum>80)daSave.dateTime.setWeather(1);
                    else if(randomNum>60)daSave.dateTime.setWeather(3);
                    else daSave.dateTime.setWeather(0);
                    daSave.dateTime.setWeatherTimeOfDuration(random.nextInt(16)+6);
                }
            }
            case 1->{//夏天
                //多雨月份
                if(daSave.dateTime.getMonth()<8){
                    if(randomNum>80)daSave.dateTime.setWeather(2);
                    else if(randomNum>60)daSave.dateTime.setWeather(1);
                    else if(randomNum>55)daSave.dateTime.setWeather(3);
                    else daSave.dateTime.setWeather(0);
                    daSave.dateTime.setWeatherTimeOfDuration(random.nextInt(24)+8);
                }else{
                    if(randomNum>85)daSave.dateTime.setWeather(2);
                    else if(randomNum>80)daSave.dateTime.setWeather(1);
                    else if(randomNum>75)daSave.dateTime.setWeather(3);
                    else daSave.dateTime.setWeather(0);
                    daSave.dateTime.setWeatherTimeOfDuration(random.nextInt(16)+6);
                }
            }
            case 2->{//秋天
                //多风月份
                if(daSave.dateTime.getMonth()<11){
                    if(randomNum>80)daSave.dateTime.setWeather(1);
                    else if(randomNum>60)daSave.dateTime.setWeather(3);
                    else daSave.dateTime.setWeather(0);
                    daSave.dateTime.setWeatherTimeOfDuration(random.nextInt(24)+8);
                }else{
                    if(randomNum>85)daSave.dateTime.setWeather(1);
                    else if(randomNum>75)daSave.dateTime.setWeather(3);
                    else daSave.dateTime.setWeather(0);
                    daSave.dateTime.setWeatherTimeOfDuration(random.nextInt(16)+6);
                }
            }
            case 3->{//冬天
                //多雪月份
                if(daSave.dateTime.getMonth()==1){
                    if(randomNum>70)daSave.dateTime.setWeather(4);
                    else if(randomNum>60)daSave.dateTime.setWeather(3);
                    else daSave.dateTime.setWeather(0);
                    daSave.dateTime.setWeatherTimeOfDuration(random.nextInt(24)+8);
                }else{
                    if(randomNum>80)daSave.dateTime.setWeather(4);
                    else if(randomNum>70)daSave.dateTime.setWeather(3);
                    else daSave.dateTime.setWeather(0);
                    daSave.dateTime.setWeatherTimeOfDuration(random.nextInt(16)+6);
                }
            }
            default -> {
                daSave.dateTime.setWeather(0);
                daSave.dateTime.setWeatherTimeOfDuration(random.nextInt(24)+8);
            }
        }
    }
}
